package priv.mill.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import priv.mill.controller.util.RetenObj;
import priv.mill.entry.Collect;
import priv.mill.server.CollectService;

@Controller
@RequestMapping("Collect")
public class CollectController {

	@Autowired
	private CollectService collectService;

	private static final Logger logger = Logger.getLogger(CollectController.class);

	@RequestMapping("getCollectById")
	@ResponseBody
	public RetenObj getCollectById(@RequestParam("CollectId") Integer collectId) {
		try {
			return warpData(collectService.getCollectById(collectId), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	private RetenObj warpData(Object object, int i) {
		RetenObj retenObj = new RetenObj();
		retenObj.setData(object);
		retenObj.setStatus(i);
		return retenObj;
	}

	@RequestMapping("getCollectByEntry")
	@ResponseBody
	public RetenObj getCollectByEntry(Collect collect) {
		try {
			return warpData(collectService.getCollectByEntry(collect), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("getCollectsByPage")
	@ResponseBody
	public RetenObj getCollectsByPage(Collect collect) {
		try {
			return warpData(collectService.getCollectsByPage(collect), 200);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("updateCollect")
	@ResponseBody
	public RetenObj updateCollect(Collect collect) {
		try {
			int result = collectService.updateCollect(collect);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}
	

	@RequestMapping("insertCollect")
	@ResponseBody
	public RetenObj insertCollect(Collect collect) {
		try {
			int result = collectService.insertCollect(collect);
			if (result > 0)
				return warpData(collect.getCId(), 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}
	}

	@RequestMapping("deleteCollect")
	
	@ResponseBody
	public RetenObj deleteCollect(Integer collectId) {
		try {
			int result = collectService.deleteCollect(collectId);
			if (result > 0)
				return warpData(result, 200);
			else
				return warpData(result, 300);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return warpData(null, 500);
		}

	}
}
